package com.fuerda.izb.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import kotlin.math.cos
import kotlin.math.sin

class HexagonView : View {

    private val paint = Paint().apply {
        color = Color.WHITE // 设置六边形的颜色
        style = Paint.Style.STROKE // 只绘制轮廓
        strokeWidth = 4f // 轮廓线宽
    }

    private val blueDecorationPaint = Paint().apply {
        color = Color.BLUE // 设置装饰性曲线的颜色
        style = Paint.Style.STROKE
        strokeWidth = 2f
    }

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    private fun init() {
        // 初始化其他属性或变量
    }

    override  fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val height = measuredHeight.toFloat()
        val width = measuredWidth.toFloat()
        val minDimension = height.coerceAtMost(width)
        val sideLength = minDimension / 2f
        val centerX = width / 2f
        val centerY = height / 2f
        val topOffset = (height - minDimension) / 2f

        // 绘制六边形
        val hexagonPath = Path()
        hexagonPath.moveTo(centerX - sideLength, centerY + topOffset)
        for (i in 1..6) {
            val angleRad = (i * Math.PI / 3).toFloat()
            val x = centerX + sideLength * cos(angleRad)
            val y = centerY + topOffset - sideLength * sin(angleRad)
            if (i == 1) {
                hexagonPath.lineTo(x, y)
            } else {
                hexagonPath.lineTo(x, y)
                if (i == 6) {
                    hexagonPath.close() // 闭合路径以形成完整的六边形
                }
            }
        }
        canvas.drawPath(hexagonPath, paint)

        // 绘制蓝色装饰性曲线（如果需要）
        // 这里你需要定义曲线的具体形状和位置，这里仅提供一个示例
        val curvePath = Path()
        curvePath.moveTo(300f,300f)
        // 使用quadTo, cubicTo等方法来定义曲线的形状
        // ...
        // 闭合路径或结束曲线的定义
        canvas.drawPath(curvePath, blueDecorationPaint)
    }

    override  fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)

        var measuredWidth = 0
        var measuredHeight = 0

        // 根据宽度和高度模式计算测量的宽度和高度
        // ...

        setMeasuredDimension(measuredWidth, measuredHeight)
    }
}
